/* 
 * File:   ImageCutter.cpp
 * Author: Guilherme Monteiro
 * 
 */

#include "../headers/ImageCutter.h"

ImageCutter::ImageCutter(IplImage* pageImg, CvPoint roiLeftU, CvPoint roiRightB) {
    this->pageImage = pageImg;
    this->ROILeftUpper = roiLeftU;
    this->ROIRightBottom = roiRightB;
}

ImageCutter::ImageCutter(const ImageCutter& orig) {
}

ImageCutter::~ImageCutter() {
}

/**
 * Cut the image
 * passed in constructor method.
 * 
 * @return image cut
 */
IplImage* ImageCutter::cut() throw (InvalidRectException) {
    CvRect rect = cvRect(this->ROILeftUpper.x, this->ROILeftUpper.y, 
            this->ROIRightBottom.x - this->ROILeftUpper.x, 
            this->ROIRightBottom.y - this->ROILeftUpper.y);
    if(!(rect.width >= 0 && 
            rect.height >= 0 && 
            rect.x < this->pageImage->width &&
            rect.y < this->pageImage->height &&
            rect.x + rect.width >= (int)(rect.width > 0) &&
            rect.y + rect.height >= (int)(rect.height > 0))) {
        throw InvalidRectException();
    }
    
    cvSetImageROI(this->pageImage, cvRect(ROILeftUpper.x, ROILeftUpper.y, 
         ROIRightBottom.x-ROILeftUpper.x, ROIRightBottom.y-ROILeftUpper.y));
    
    return this->pageImage;
}

